package cn.joyfulmedia.mis.utils;


import cn.joyfulmedia.common.dto.ResultDO;
import cn.joyfulmedia.common.util.Const;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by darren.dai on 15/05/2017.
 */
public class MisResponse {

    /**
     * MIS 操作成功
     *
     * @return
     */
    public static Map<String, Object> getSuccessMap() {
        return getResponseMap(0, "操作成功！");
    }

    /**
     * MIS 操作结果对象
     *
     * @param status
     * @param msg
     * @return
     */
    public static Map<String, Object> getResponseMap(int status, String msg) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put(Const.STATUS, status);
        resultMap.put(Const.MSG, msg);
        return resultMap;
    }

    /**
     * 更新请求返回值
     *
     * @param responseMap
     * @param status
     * @param msg
     */
    public static void updateResponseMap(Map<String, Object> responseMap, int status, String msg) {
        responseMap.put(Const.STATUS, status);
        if (StringUtils.isNotBlank(msg)) {
            responseMap.put(Const.MSG, msg);
        }
    }

    public static Map<String, Object> updateResponseMapReturnBack(Map<String, Object> responseMap, int status, String msg) {
        responseMap.put(Const.STATUS, status);
        if (StringUtils.isNotBlank(msg)) {
            responseMap.put(Const.MSG, msg);
        }
        return responseMap;
    }

    /**
     * 判断结果是否为成功
     *
     * @param responseMap
     * @return
     */
    public static boolean isSuccess(Map<String, Object> responseMap) {
        boolean result = false;
        if (responseMap.get(Const.STATUS).toString().equals("0")) {
            result = true;
        }
        return result;
    }

    /**
     * 处理错误的结果集
     *
     * @param resultDO
     * @param responseMap
     */
    public static void handleResultDO2ResponseMap(ResultDO resultDO, Map<String, Object> responseMap) {
        if (ResultDO.isError(resultDO)) {
            updateResponseMap(responseMap, -1, resultDO.getErrorMesssage());
        }
    }
}
